package Components.DAO.common;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionTemplate;

import Components.DAO.common.iface.DBProxyInterface;

@Repository("dbProxyInterface")
public class Hiber_DatabaseDao implements DBProxyInterface {

	@Autowired
	private HibernateTransactionManager hibernateTransactionManager;

	private TransactionTemplate transactionTemplate;

	/**
	 * check if the specific user existing according to the input parameters.<br/>
	 * 
	 * @param Buser_id
	 * @return Buser
	 * 
	 *         public Buser queryUserFrom(int Buser_id) { Buser buser = null;
	 *         Session session =
	 *         this.hibernateTransactionManager.getSessionFactory()
	 *         .openSession(); Query query = session
	 *         .createQuery("from Buser t where t.BUSER_ID=? and t.valid=1 ");
	 *         query.setBigDecimal(1, new BigDecimal(Buser_id));
	 * 
	 *         List<Buser> list = query.list(); if (!(list.isEmpty() ||
	 *         list.size() > 1)) { buser = list.get(0); } return buser; }
	 */

	public Boolean save(Object obj) throws Exception {
		boolean isSuccess = false;
		Session session = null;
		session = this.hibernateTransactionManager.getSessionFactory()
				.openSession();
		session.beginTransaction();
		session.saveOrUpdate(obj);
		session.getTransaction().commit();
		session.close();
		isSuccess = true;
		return isSuccess;
	}

	@Override
	public List<Object> query(String hqlString, Object[] objects) {
		Object rtnObj = null;
		Session session = this.hibernateTransactionManager.getSessionFactory()
				.openSession();
		Query query = session.createQuery(hqlString);
		int length = objects.length;
		for (int j = 0; j < length; j++) {
			query.setParameter(j, objects[j]);
		}
		List<Object> list = query.list();
		return list;
	}

}
